package sg.edu.nus.iss.billing.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.SimpleDateFormat;

import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;

import sg.edu.nus.iss.billing.entity.CustomerComplaint;
import sg.edu.nus.iss.billing.localization.MessageKeyManager;

public class ComplaintSummaryDetailsPanel extends JPanel {

  /**
	 * 
	 */
  private static final long serialVersionUID = 7672593279496146477L;
  boolean expander = false;
  JEditorPane editor;
  JPanel contentPanel;
  JLabel expanderLabel;
  CustomerComplaint complaint;
  // call back when user clicks on the update button
  private ActionListener listener;

  void updateComplaint() {
    if (this.listener != null) {
      ActionEvent event = new ActionEvent(this.complaint, 0, "");
      this.listener.actionPerformed(event);
    }
  }
  
  void showHideText() {
    if (ComplaintSummaryDetailsPanel.this.expander == false) {
      this.expanderLabel.setText("-");
      ComplaintSummaryDetailsPanel.this.expander = true;
      GridBagHelper.addComponent(
          ComplaintSummaryDetailsPanel.this.contentPanel,
          ComplaintSummaryDetailsPanel.this.editor, 1, 1, 5,
          GridBagHelper.INSETS_NONE);
    } else {
      this.expanderLabel.setText("+");
      ComplaintSummaryDetailsPanel.this.expander = false;
      ComplaintSummaryDetailsPanel.this.contentPanel
          .remove(ComplaintSummaryDetailsPanel.this.editor);
    }
  }
  
  /**
   * Create the panel.
   */
  public ComplaintSummaryDetailsPanel(CustomerComplaint myComplaint,
      boolean header, ActionListener myListener) {
    // save the complaint locally
    this.complaint = myComplaint;
    this.listener = myListener;

    // use the border layout for the main panel
    setLayout(new BorderLayout(0, 0));

    // store the actual contents in the content panel
    this.contentPanel = new JPanel();
    add(this.contentPanel, BorderLayout.NORTH);

    // create the layout for the content panel
    GridBagLayout gbl_contentPanel = new GridBagLayout();
    gbl_contentPanel.columnWidths = new int[] { 20, 100, 100, 120, 100, 210, 80, 0, 0 };
    gbl_contentPanel.rowHeights = new int[] { 24, 0, 0 };
    gbl_contentPanel.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
        1.0, Double.MIN_VALUE };
    gbl_contentPanel.rowWeights = new double[] { 0.0, 1.0, Double.MIN_VALUE };
    this.contentPanel.setLayout(gbl_contentPanel);

    if (header == false) {
      this.editor = new JEditorPane();
      this.editor.setEditable(false);
      this.editor.setText(this.complaint.getComplaints());
  
      this.expanderLabel = new JLabel("+");
      this.expanderLabel.addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent me) {
          showHideText();
        }
      });
    }

    // populate the strings
    String[] values;
    if (header == false) {
      SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
      String formattedDate = formatter.format(this.complaint.getReportedDate());
      values = new String[] { this.complaint.getComplaintRefKey(),
          this.complaint.getNRIC(), 
          formattedDate,
          this.complaint.getStatus() + "", 
          this.complaint.getCategory() + "" };
    } else {
      values = new String[] { MessageKeyManager.getMessage("label.complaintID"),
          MessageKeyManager.getMessage("label.NRIC/FIN"),
          MessageKeyManager.getMessage("label.reportedDate"),
          MessageKeyManager.getMessage("label.status"),
          MessageKeyManager.getMessage("label.category")};
    }

    // add the expander label
    if (header == false) {
      GridBagHelper.addComponent(this.contentPanel, this.expanderLabel, 0, 0, 0,
          GridBagHelper.INSETS_NONE);
      showHideText();
    }

    // add the various columns
    int i = 0;
    for ( ; i < values.length; i++) {
      JPanel panel = (JPanel) GridBagHelper.addComponent(this.contentPanel,
          new JPanel(), i + 1, 0, 0, GridBagHelper.INSETS_NONE);
      panel.setBorder(new LineBorder(new Color(0, 0, 0)));
      GridBagLayout layout = new GridBagLayout();
      layout.columnWidths = new int[] { 0, 0 };
      layout.rowHeights = new int[] { 0, 0, 0 };
      layout.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE };
      layout.rowWeights = new double[] { 0.0, 1.0, Double.MIN_VALUE };
      panel.setLayout(new GridBagLayout());
      GridBagHelper.addLabel(panel, values[i], SwingConstants.RIGHT, 0, 0, GridBagConstraints.WEST, 0, GridBagHelper.INSETS_NONE);
    }

    // add the update button
    if (header == false) {
      // create the update button and add it to the content panel
      JButton btnUpdate = new JButton(
          MessageKeyManager.getMessage("button.update"));
      btnUpdate.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
          updateComplaint();
        }
      });
      GridBagHelper.addComponent(this.contentPanel, btnUpdate, ++i, 0, 0,
          GridBagHelper.INSETS_NONE);
    }

  }
}